package trivia;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.text.DecimalFormat;
import java.util.Vector;

import javax.swing.JList;

import score.Score;
import score.Scorelist;

public class ScoreFrame {

	private JFrame jScoreFrame = null;  //  @jve:decl-index=0:visual-constraint="243,3"
	private JPanel jScoreFrameContentPane = null;
	private JList jScoreList = null;
	
	public ScoreFrame (Scorelist l, String diffic) {
		jScoreFrame = getJScoreFrame();
		setScoreList(l, diffic);
		
	}

	private void setScoreList(Scorelist l, String diffic) {
		if( diffic.equals("Easy") )  {
			jScoreList.setListData( listToShow(l.getFacil()));
		}
		else if( diffic.equals("Normal")) {
				jScoreList.setListData( listToShow(l.getNormal()));
		}
		else {
				jScoreList.setListData(listToShow( l.getDificil()));
		}
				
	}
	
	private String[] listToShow( Vector<Score> v ){
		String[] vec = new String[11];
		
		vec[0] = " Users                            Scores       Difficulty";
		for(int i = 1; i < 11; i++ ){
			vec [i] = " "+((Score)(v.get(i - 1))).getUserName() + "   ";
			vec[i] += "        " + roundTwoDecimals(((Score)(v.get(i - 1))).getScore())+ "%    ";
			vec[i] += "        " + ((Score)(v.get(i - 1))).getDiffic()+ "    ";
		}
		return vec;
	}
		
	
	/**
	 * This method initializes jScoreFrame	
	 * 	
	 * @return javax.swing.JFrame	
	 */
	public JFrame getJScoreFrame() {
		if (jScoreFrame == null) {
			jScoreFrame = new JFrame();
			jScoreFrame.setSize(new Dimension(445, 239));
			jScoreFrame.setContentPane(getJScoreFrameContentPane());
		}
		return jScoreFrame;
	}

	/**
	 * This method initializes jScoreFrameContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJScoreFrameContentPane() {
		if (jScoreFrameContentPane == null) {
			jScoreFrameContentPane = new JPanel();
			jScoreFrameContentPane.setLayout(new BorderLayout());
			jScoreFrameContentPane.add(getJScoreList(), BorderLayout.CENTER);
		}
		return jScoreFrameContentPane;
	}

	/**
	 * This method initializes jScoreList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getJScoreList() {
		if (jScoreList == null) {
			jScoreList = new JList();
		}
		return jScoreList;
	}
	
	String roundTwoDecimals(double d) {
    	DecimalFormat twoDForm = new DecimalFormat("#.##");
    	
    	//return Double.parseDouble(twoDForm.format(d).toString());
    	return twoDForm.format(d);
	}

}
